local GM = GM or gmod.GetGamemode()

function GM:RegisterSvar(svartitle,default,callback,...)
	local self = gmod.GetGamemode()
	local svartitle = "ws_" .. svartitle
	self.svar[svartitle] = self.svar[svartitle] or default
	if callback then
		self.svar[svartitle].cb = callback
	end
	concommand.Add(svartitle,WSvarcommand,...)
end

function WSvarCommand(pl,command,args)
	local self = gmod.GetGamemode()
	
	if #args >= 2 then
		local v = table.remove(args,1)
		local v = string.Implode(" ",v)
	else
		local v = args[1]
	end
	
	self.svar[command] = args[1]
	if self.svar[command].cb then
		table.insert(v,1,self.svar[command].cb)
		pcall(unpack(v))
	end
	if type(args[1]) == "long" then
		umsg.Start("wsSvarClUpdate", rp)
			umsg.String( command )
			umsg.Long( args[1] )
		umsg.End() 
	else
		umsg.Start("wsSvarClUpdate", rp)
			umsg.String( command )
			umsg.String( v )
		umsg.End() 
	end
end

function WSvarClientUpdate(msg)
	if SERVER then return end
	local key = msg:ReadString()
	local value = msg:ReadString() or msg:ReadLong()
	local self = gmod.GetGamemode()
	self.svar[key] = value
	//Todo client callbacks
end

usermessage.Hook("wsSvarClUpdate", WSvarClientUpdate)  